package com.allchips.pin.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.allchips.pin.domain.OriginalStandardValue;
import com.allchips.pin.domain.PinInfoValue;
import com.allchips.pin.enums.DeleteFlagEnum;
import com.allchips.pin.enums.Original2StandardSyncEnum;
import com.allchips.pin.service.OriginalStandardValueService;
import com.allchips.pin.service.PinInfoLcService;
import com.allchips.pin.service.PinInfoValueService;
import com.allchips.pin.util.GenericResp;
import com.allchips.pin.util.RestAPICode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/originalStandard")
public class OriginalStandardValueController {

    @Resource
    OriginalStandardValueService originalStandardValueService;

    @Resource
    PinInfoLcService pinInfoLcService;

    @Resource
    PinInfoValueService pinInfoValueService;


    @PostMapping("/list")
    public GenericResp<com.allchips.pin.util.Page<OriginalStandardValue>> list(@RequestBody com.allchips.pin.util.Page<OriginalStandardValue> page) {
        OriginalStandardValue param = ObjectUtil.defaultIfNull(page.getParam(), OriginalStandardValue::new);
        LambdaQueryWrapper<OriginalStandardValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(param.getOriginal()), OriginalStandardValue::getOriginal, param.getOriginal());
        queryWrapper.eq(StrUtil.isNotBlank(param.getStandard()), OriginalStandardValue::getStandard, param.getStandard());
        queryWrapper.eq(OriginalStandardValue::getDeleteFlag, DeleteFlagEnum.NO.getStatusCode());
        queryWrapper.orderByDesc(OriginalStandardValue::getCreateTime);
        Page<OriginalStandardValue> queryPage = originalStandardValueService.page(new Page<>(page.getPageNo(), page.getPageSize()), queryWrapper);
        page.setTotalCount(queryPage.getTotal());
        page.setResult(queryPage.getRecords());
        GenericResp<com.allchips.pin.util.Page<OriginalStandardValue>> resp = new GenericResp<>();
        resp.setData(page);
        return resp;
    }

    @PostMapping("/save")
    public GenericResp<Void> save(@RequestBody OriginalStandardValue originalStandardValue) {
        GenericResp<Void> resp = new GenericResp<>();
        if (originalStandardValue == null || originalStandardValue.getId() != null || StrUtil.isBlank(originalStandardValue.getOriginal()) || StrUtil.isBlank(originalStandardValue.getStandard())) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("参数不正确");
            return resp;
        }
        //标准值不能存在原始值中
        if (Objects.equals(originalStandardValue.getOriginal(),originalStandardValue.getStandard())) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("原始值不能等于标准值");
            return resp;
        }

        //标准值不能存在原始值中
        if (standardNotInOriginal(originalStandardValue)) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("标准值不能存在原始值中");
            return resp;
        }

        //原始值不能重复
        if (originalNotRepeat(originalStandardValue)) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("原始值不能重复");
            return resp;
        }
        //原始值不能存在于标准值
        if (originalNotInStandard(originalStandardValue)) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("原始值不能存在于标准值");
            return resp;
        }

        originalStandardValue.setSync(Original2StandardSyncEnum.AUDIT.getStatusCode());
        originalStandardValue.setCreateTime(new Date());
        originalStandardValue.setUpdateTime(originalStandardValue.getCreateTime());
        originalStandardValueService.save(originalStandardValue);
        return resp;
    }

    /**
     * 修改只能修改原始值
     */
    @PostMapping("/update")
    public GenericResp<Void> update(@RequestBody OriginalStandardValue originalStandardValue) {
        GenericResp<Void> resp = new GenericResp<>();
        if (originalStandardValue == null || originalStandardValue.getId() == null || StrUtil.isBlank(originalStandardValue.getOriginal()) || StrUtil.isBlank(originalStandardValue.getStandard())) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("参数不正确");
            return resp;
        }
        //获取数据
        final OriginalStandardValue byId = originalStandardValueService.getById(originalStandardValue.getId());
        if (byId == null || byId.getDeleteFlag() == DeleteFlagEnum.YES.getStatusCode()) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("数据不存在");
            return resp;
        }
        //标准值不能存在原始值中
        if (Objects.equals(originalStandardValue.getOriginal(),originalStandardValue.getStandard())) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("原始值不能等于标准值");
            return resp;
        }
        //原始值不能重复
        if (originalNotRepeat(originalStandardValue)) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("原始值不能重复");
            return resp;
        }
        //原始值不能存在于标准值
        if (originalNotInStandard(originalStandardValue)) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("原始值不能存在标准值中");
            return resp;
        }
        //标准值不能存在原始值中
        if (standardNotInOriginal(originalStandardValue)) {
            resp.setCode(RestAPICode.DATABASE_ERROR.getCode());
            resp.setMessage("标准值不能存在原始值中");
            return resp;
        }
        OriginalStandardValue update = new OriginalStandardValue();
        update.setId(originalStandardValue.getId());
        update.setUpdateTime(new Date());
        update.setOriginal(originalStandardValue.getOriginal());
        update.setStandard(originalStandardValue.getStandard());
        //修改后状态改为未同步
        update.setSync(Original2StandardSyncEnum.NO.getStatusCode());
        originalStandardValueService.updateById(update);
        return resp;
    }

    /**
     * 审核通过接口
     */
    @PostMapping("/audit")
    public GenericResp<Void> audit(@RequestBody OriginalStandardValue originalStandardValue) {
        GenericResp<Void> resp = new GenericResp<>();
        if (originalStandardValue == null || originalStandardValue.getId() == null) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("参数不正确");
            return resp;
        }
        //获取数据
        OriginalStandardValue byId = originalStandardValueService.getById(originalStandardValue.getId());
        if (byId == null || byId.getDeleteFlag() == DeleteFlagEnum.YES.getStatusCode()) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("数据不存在");
            return resp;
        }
        if (byId.getSync() != Original2StandardSyncEnum.AUDIT.getStatusCode()) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("只有待审核状态才允许操作");
            return resp;
        }
        OriginalStandardValue update = new OriginalStandardValue();
        update.setId(originalStandardValue.getId());
        update.setSync(Original2StandardSyncEnum.NO.getStatusCode());
        originalStandardValueService.updateById(update);
        return resp;
    }


    @PostMapping("/count")
    public GenericResp<Dict> count(@RequestBody OriginalStandardValue originalStandardValue) {
        GenericResp<Dict> resp = new GenericResp<>();
        if (originalStandardValue == null || (StrUtil.isBlank(originalStandardValue.getOriginal()) && StrUtil.isBlank(originalStandardValue.getStandard()))) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("参数不正确");
            return resp;
        }
        Dict dict = Dict.create();
        if (StrUtil.isNotBlank((originalStandardValue.getOriginal()))) {
            LambdaQueryWrapper<PinInfoValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PinInfoValue::getValue, originalStandardValue.getOriginal());
            long count = pinInfoValueService.count(queryWrapper);
            dict.put("original", count);
        }

        if (StrUtil.isNotBlank((originalStandardValue.getStandard()))) {
            LambdaQueryWrapper<PinInfoValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PinInfoValue::getValue, originalStandardValue.getStandard());
            long count = pinInfoValueService.count(queryWrapper);
            dict.put("standard", count);
        }
        resp.setData(dict);
        return resp;
    }

    @PostMapping("/delete")
    public GenericResp<Void> delete(@RequestBody OriginalStandardValue originalStandardValue) {
        GenericResp<Void> resp = new GenericResp<>();
        if (originalStandardValue.getId() == null) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("参数不正确");
            return resp;
        }
        final OriginalStandardValue byId = originalStandardValueService.getById(originalStandardValue.getId());
        if (byId == null || byId.getDeleteFlag() == DeleteFlagEnum.YES.getStatusCode()) {
            resp.setCode(RestAPICode.REQUEST_PARAM_FIELD_ERROR.getCode());
            resp.setMessage("数据不存在");
            return resp;
        }
        OriginalStandardValue deleteData = new OriginalStandardValue();
        deleteData.setId(originalStandardValue.getId());
        deleteData.setDeleteFlag(DeleteFlagEnum.YES.getStatusCode());
        deleteData.setUpdateTime(new Date());
        originalStandardValueService.updateById(deleteData);
        return resp;
    }

    /**
     * 标准值不能存在原始值中
     */
    private boolean standardNotInOriginal(OriginalStandardValue originalStandardValue) {
        LambdaQueryWrapper<OriginalStandardValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OriginalStandardValue::getOriginal, originalStandardValue.getStandard());
        lambdaQueryWrapper.eq(OriginalStandardValue::getDeleteFlag, DeleteFlagEnum.NO.getStatusCode());
        return originalStandardValueService.count(lambdaQueryWrapper) > 0;
    }

    /**
     * 原始值不能重复
     */
    private boolean originalNotRepeat(OriginalStandardValue originalStandardValue) {
        LambdaQueryWrapper<OriginalStandardValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(originalStandardValue.getId() != null, OriginalStandardValue::getId, originalStandardValue.getId());
        lambdaQueryWrapper.eq(OriginalStandardValue::getOriginal, originalStandardValue.getOriginal());
        lambdaQueryWrapper.eq(OriginalStandardValue::getDeleteFlag, DeleteFlagEnum.NO.getStatusCode());
        return originalStandardValueService.count(lambdaQueryWrapper) > 0;
    }

    /**
     * 原始值不能存在标准值中
     */
    private boolean originalNotInStandard(OriginalStandardValue originalStandardValue) {
        LambdaQueryWrapper<OriginalStandardValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(originalStandardValue.getId() != null, OriginalStandardValue::getId, originalStandardValue.getId());
        lambdaQueryWrapper.eq(OriginalStandardValue::getStandard, originalStandardValue.getOriginal());
        lambdaQueryWrapper.eq(OriginalStandardValue::getDeleteFlag, DeleteFlagEnum.NO.getStatusCode());
        return originalStandardValueService.count(lambdaQueryWrapper) > 0;
    }


}